//
//  MainTableViewCell.m
//  二级评论
//
//  Created by Andrew on 2018/1/19.
//  Copyright © 2018年 Andrew. All rights reserved.
//

#import "MainTableViewCell.h"
#import "FamilyGroupFrame.h"
#import "FamilyGroup.h"
#import "YMStringSize.h"
#import "CommentTableViewCell.h"
#import "SDPhotoGroup.h"

@interface MainTableViewCell () <UITableViewDelegate,UITableViewDataSource>

/* origalView */
@property (nonatomic, weak) UIView *origalView;
/** 头像 */
@property (weak,nonatomic) UIImageView *iconView;
/** 名称 */
@property (weak,nonatomic) UILabel *nameLabel;
/** 时间 */
@property (weak,nonatomic) UILabel *timeLabel;
/** 内容 */
@property (weak,nonatomic) UILabel *shuoshuotextLabel;
/* 配图 */
@property (nonatomic, weak) SDPhotoGroup *photoGroupView;
/* 分割线 */
@property (nonatomic, weak) UIView *lineView;
/* 评论背景 */
@property (nonatomic, weak) UIView *commentsBackView;
/* 评论 */
@property (nonatomic, weak) UITableView *tableView;
/* 评论按钮 */
@property (nonatomic, weak) UIButton *commentBtn;


@end


@implementation MainTableViewCell

#define LeftMargin 10
#define TopMargin 10

+ (instancetype)cellWithTableVie:(UITableView *)tableView
{
    static NSString *cellID = @"MainTableViewCell";
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        /* origalView */
        UIView *origalView = [[UIView alloc] init];
        [self addSubview:origalView];
        self.origalView = origalView;
        
        /** 头像 */
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.origalView addSubview:iconView];
        self.iconView = iconView;
        
        /** 名称 */
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.font = LabelFontRegular(20);
        [self.origalView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        /** 内容 */
        UILabel *shuoshuotextLabel = [[UILabel alloc] init];
        shuoshuotextLabel.font = LabelFontRegular(16);
        shuoshuotextLabel.numberOfLines = 0;
        [self.origalView addSubview:shuoshuotextLabel];
        self.shuoshuotextLabel = shuoshuotextLabel;
        
        /* 配图 */
        SDPhotoGroup *photoGroupView = [[SDPhotoGroup alloc] init];
        [self.origalView addSubview:photoGroupView];
        self.photoGroupView = photoGroupView;
        
        /* 评论背景 */
        UIView *commentsBackView = [[UIView alloc] init];
        commentsBackView.backgroundColor = YMGrayColor(221);
        [self.origalView addSubview:commentsBackView];
        self.commentsBackView = commentsBackView;
        
        /* 评论 */
        UITableView *tableView = [[UITableView alloc] init];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.showsHorizontalScrollIndicator = NO;
        tableView.scrollEnabled = NO;
        tableView.backgroundColor = YMGrayColor(221);
        [self.commentsBackView addSubview:tableView];
        self.tableView = tableView;
        
        /** 时间 */
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.font = LabelFontRegular(14);
        [self.origalView addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        /* 评论按钮 */
        UIButton *commentBtn = [[UIButton alloc] init];
        commentBtn.backgroundColor = YMColor(247, 148, 30);
        [commentBtn setTitle:@"评论" forState:UIControlStateNormal];
        [commentBtn setTitleColor:YMColor(52, 52, 52) forState:UIControlStateNormal];
        commentBtn.titleLabel.font = LabelFontRegular(14);
        commentBtn.layer.cornerRadius = 5;
        commentBtn.layer.masksToBounds = YES;
        [commentBtn addTarget:self action:@selector(commentBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.origalView addSubview:commentBtn];
        self.commentBtn = commentBtn;
        
        /* 分割线 */
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = YMGrayColor(201);
        [self.origalView addSubview:lineView];
        self.lineView = lineView;
    }
    
    return self;
}

- (void)commentBtnClick:(UIButton *)sender
{
    if (self.commentBringParamBlock) {
        self.commentBringParamBlock(self.indexPath);
    }
}

- (void)setFrameModel:(FamilyGroupFrame *)frameModel
{
    _frameModel = frameModel;
    
    /* origalView */
    self.origalView.frame = frameModel.origalViewF;
    
    /** 头像 */
    self.iconView.frame = frameModel.iconViewF;
    self.iconView.image = [UIImage imageNamed:frameModel.model.icon];
    
    /** 名称 */
    self.nameLabel.frame = frameModel.nameLabelF;
    self.nameLabel.text = frameModel.model.name;
    
    /** 内容 */
    self.shuoshuotextLabel.frame = frameModel.shuoshuotextLabelF;
    self.shuoshuotextLabel.text = frameModel.model.shuoshuoText;
    
    /* 配图 */
    self.photoGroupView.frame = frameModel.photoGroupViewF;
    self.photoGroupView.photoItemArray = frameModel.model.pictures;
    
    /* 评论背景 */
    self.commentsBackView.frame = frameModel.commentsBackViewF;
    
    /* 评论 */
    self.tableView.frame = CGRectMake(0, 0, self.commentsBackView.ym_width, self.commentsBackView.ym_height);
    [self.tableView reloadData];
    
    /** 时间 */
    self.timeLabel.frame = frameModel.timeLabelF;
    self.timeLabel.text = frameModel.model.time;
    
    /* 评论按钮 */
    self.commentBtn.frame = frameModel.commentBtnF;
    
    /* 分割线 */
    self.lineView.frame = frameModel.lineViewF;
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.frameModel.model.replys.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.frameModel.model.replys.count) {
        
        CGFloat W = SCREENWIDTH - LeftMargin * 2;
        CGFloat margin = 5 + 5;//上下边距
        CGFloat cellHeight = [YMStringSize ym_sizeWithText:self.frameModel.model.replys[indexPath.row] font:LabelFontRegular(16) maxW:W].height + margin;
        return cellHeight;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    CommentTableViewCell *cell = [CommentTableViewCell cellWithTableVie:tableView];
    cell.backgroundColor = YMGrayColor(221);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.commentStr = self.frameModel.model.replys[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取朋友的名称
    CommentTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSArray *comment = [cell.commentStr componentsSeparatedByString:@"："];
    NSString *friendName = comment[0];
    
    if (!([friendName rangeOfString:@"回复"].location == NSNotFound)) { //有回复
        NSArray *friend = [friendName componentsSeparatedByString:@"回复"];
        friendName = friend[1];
    }
    
    if (self.commentReplayBlock) {
        self.commentReplayBlock(self.indexPath, friendName);
    }
}

@end
